import java.util.ArrayList;
//import Appointment;
import java.util.Calendar;

public class AppointmentCalendar {
	ArrayList<Appointment> calendarOfAppointments = new ArrayList<Appointment>();
	
	public void scheduleAnAppointment(Appointment appointment) throws ConcurrentAppException{
		for (Appointment existingAppointment : calendarOfAppointments) {
			if (doesNotConflict(appointment, existingAppointment)){
					calendarOfAppointments.remove(existingAppointment);
					calendarOfAppointments.add(appointment);
			}else
				throw new ConcurrentAppException();
		}
		calendarOfAppointments.add(appointment);
	}

	private boolean doesNotConflict(Appointment appointment, Appointment existingAppointment) {
		return (appointment.overlaps(existingAppointment))&&(appointment.returnImportance(appointment)> existingAppointment.returnImportance(existingAppointment));
	}

	public void deleteAnAppointment(Appointment appointment) {
		calendarOfAppointments.remove(appointment);
	}


	public boolean containsAppointment(Appointment appointment) {
		if (calendarOfAppointments.contains(appointment))
			return true;
		else
			return false;
	}

	public Appointment currentAppointments() {;
		for (Appointment appointment: calendarOfAppointments){
			if (appointment.start.before(Calendar.getInstance()) && appointment.end.after(Calendar.getInstance()) || appointment.start.equals(Calendar.getInstance()) || appointment.end.equals(Calendar.getInstance())){
				return appointment;
			}
		}
		return null;
	}

}
